/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.config.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.common.core.page.PageResponse;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.cloud.provider.punish.config.dto.PhraseQueryPageDto;
import com.icinfo.cloud.provider.punish.config.dto.PhraseSaveDto;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigCasePhrasesMapper;
import com.icinfo.cloud.provider.punish.config.model.ConfigCasePhrases;
import com.icinfo.cloud.provider.punish.config.service.IConfigCasePhrasesService;
import com.icinfo.cloud.provider.punish.config.vo.PhraseCategoryVo;
import com.icinfo.cloud.provider.punish.config.vo.PhraseVo;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.mybatis.pagehelper.PageHelper;
import com.icinfo.framework.tools.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 描述: 常用短语表 case_approval_phrases 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年02月16日
 */
@Service
public class ConfigCasePhrasesServiceImpl extends MyBatisServiceSupport implements IConfigCasePhrasesService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(ConfigCasePhrasesServiceImpl.class);

    @Autowired
    private ConfigCasePhrasesMapper caseApprovalPhrasesMapper;

    @Override
    public PageResponse<PhraseVo> getList(PhraseQueryPageDto phraseQueryPageDto){
        // 分页
        UserDTO userDTO = UserUtil.getUserInfo();
        Example example  = new Example(ConfigCasePhrases.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("creator",userDTO.getUserId().toString());
        if (StringUtils.isNotBlank(phraseQueryPageDto.getTitle())){
            criteria.andLike("title","%"+phraseQueryPageDto.getTitle()+"%");
        }
        if (StringUtils.isNotBlank(phraseQueryPageDto.getCategory())){
            criteria.andEqualTo("category",phraseQueryPageDto.getCategory());
        }
        example.orderBy("updateTime").desc().orderBy("sortNo").desc();
        PageHelper.startPage(phraseQueryPageDto.getPageNum(), phraseQueryPageDto.getLength());
        List<ConfigCasePhrases> phrases = caseApprovalPhrasesMapper.selectByExample(example);
        List<PhraseCategoryVo> categoryList = caseApprovalPhrasesMapper.getPhraseCategoryList();
        //transfer
        List<PhraseVo> phraseVos = new ArrayList<>();
        for (ConfigCasePhrases phrase: phrases) {
            PhraseVo phraseVo = new PhraseVo();
            BeanUtil.copyProperties(phrase,phraseVo);
            for (PhraseCategoryVo categoryVo : categoryList){
                if (phrase.getCategory().equals(categoryVo.getCode())){
                    phraseVo.setCategoryName(categoryVo.getName());
                }
            }
            phraseVos.add(phraseVo);
        }
        PageResponse<ConfigCasePhrases> originPage = new PageResponse<>(phrases);
        PageResponse<PhraseVo> Page  = new PageResponse<>();
        Page.setRecordsTotal(originPage.getRecordsTotal());
        //Page.setPages(originPage.getPages());
        Page.setData(phraseVos);
        return Page;
    }

    @Override
    public List<PhraseCategoryVo> getPhraseCategoryList() {
        List<PhraseCategoryVo> vos = caseApprovalPhrasesMapper.getPhraseCategoryList();
        for (PhraseCategoryVo vo : vos){
            vo.setMaxSort(caseApprovalPhrasesMapper.getMaxSort(vo.getCode()));
        }
        return vos;
    }

    @Override
    public boolean savePhrase(PhraseSaveDto casePhrases) {
        ConfigCasePhrases saveEntity = new ConfigCasePhrases();
        BeanUtil.copyProperties(casePhrases,saveEntity);
        saveEntity.setCreateTime(new Date());
        saveEntity.setUpdateTime(new Date());
        UserDTO userDto = UserUtil.getUserInfo();
        saveEntity.setCreator(userDto.getUserId().toString());
        saveEntity.setUpdator(userDto.getUserId().toString());
        saveEntity.setId(StringUtils.uuid());
        return caseApprovalPhrasesMapper.insert(saveEntity) == 1;
    }

    @Override
    public boolean modifyPhrase(PhraseSaveDto casePhrases) {
        //参数校验
        if (StringUtils.isBlank(casePhrases.getId())){
            return false;
        }
        //数据校验
        ConfigCasePhrases updateEntity = caseApprovalPhrasesMapper.selectByPrimaryKey(casePhrases.getId());
        if (updateEntity == null){
            return false;
        }
        // fill fields
        UserDTO userDto = UserUtil.getUserInfo();
        updateEntity.setUpdator(userDto.getUserId().toString());
        updateEntity.setUpdateTime(new Date());
        updateEntity.setSortNo(casePhrases.getSortNo());
        updateEntity.setTitle(casePhrases.getTitle());
        updateEntity.setContent(casePhrases.getContent());
        updateEntity.setCategory(casePhrases.getCategory());
        return caseApprovalPhrasesMapper.updateByPrimaryKey(updateEntity) == 1;
    }

    @Override
    public boolean removePhrase(String caseId) {
        ConfigCasePhrases updateEntity = caseApprovalPhrasesMapper.selectByPrimaryKey(caseId);
        if (updateEntity == null){
            return false;
        }
        return caseApprovalPhrasesMapper.deleteByPrimaryKey(caseId) == 1;
    }

    @Override
    public PhraseVo getPhraseDetail(String id) {
        ConfigCasePhrases configCasePhrases = caseApprovalPhrasesMapper.selectByPrimaryKey(id);
        PhraseVo phraseVo = new PhraseVo();
        if (configCasePhrases != null){
            BeanUtil.copyProperties(configCasePhrases,phraseVo);
        }
        return phraseVo;
    }
}